package controllers;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.UsersInfo;

import models.Area;
import models.Benefit;
import models.Customer;
import models.Location;
import models.PostQuestion;
import models.ReplyQuestion;
import models.SolutionSubmission;
import models.SolutionSubscription;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.common.eventbus.Subscribe;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class ajxLoadCheckBoxServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		PrintWriter out = resp.getWriter();
        UserService userService = UserServiceFactory.getUserService();
        String userEmailID = userService.getCurrentUser().getEmail(); 
		
        String KeyTemp = req.getParameter("Key");
        String CatTemp = req.getParameter("Cat");
        
		String  htmlstr = "", tempstr = "";
		int counter = 1, row = 2;
		boolean subscribed = false;
		
		if (KeyTemp.equalsIgnoreCase("CustomersInfo")) {
			htmlstr = "<div class='lotusTabContainer'  ><ul class='lotusTabs'><li class ='lotusSelected'><a  href='javascript:CustomersInfo()'>Customers</a></li><li ><a href='javascript:ProcessInfo()' >Process Area</a></li><li><a  href='javascript:BenefitsInfo()'>Benefits</a></li><li ><a href='javascript:SitesInfo()'>Locations</a></li></ul></div>";
			htmlstr	=	htmlstr+ "<table>";

			Objectify ofy = ObjectifyService.begin();
			Query<Customer> q = ofy.query(Customer.class).order("custIndex");
			for ( Customer f : q.fetch() ) {
				Query<SolutionSubscription> qq = ofy.query(SolutionSubscription.class).filter("custID", f.getCustID()).filter("subscribeBY", userEmailID);
				subscribed = false;
				for ( SolutionSubscription ff : qq.fetch() ) {
					subscribed = true;
					break;
				}
				if ( row == 2) {
					htmlstr	=	htmlstr +	"<TR>";
				}
				
				if (subscribed)
				{
					htmlstr	=	htmlstr +	"<TD><INPUT id=\"chk"+ f.getCustIndex()+ "\" onclick=\"UnSubscribeDocValue('" + f.getCustID() + "','" + f.getCustIndex() + "')\" type=checkbox name=chk><FONT style='FONT-WEIGHT: bold' color=#fdb813>Subscribed </FONT>" + f.getCustName() + "</TD>";
				}else{
					htmlstr	=	htmlstr +	"<TD><INPUT id=\"chk"+ f.getCustIndex()+ "\" onclick=\"StoreDocValue('" + f.getCustID() + "','" + f.getCustIndex() + "')\" type=checkbox name=chk>" + f.getCustName() + "</TD>";
				}
				
				row++;
				
				if ( row == 4){
					htmlstr	=	htmlstr +	"</TR>";
					row = 0;
				}
				
			}
			
		}else if (KeyTemp.equalsIgnoreCase("ProcessInfo")) {
			htmlstr = "<div class='lotusTabContainer'  ><ul class='lotusTabs'><li><a  href='javascript:CustomersInfo()'>Customers</a></li><li class ='lotusSelected' ><a href='javascript:ProcessInfo()' >Process Area</a></li><li><a  href='javascript:BenefitsInfo()'>Benefits</a></li><li ><a href='javascript:SitesInfo()'>Locations</a></li></ul></div>";		
			htmlstr	=	htmlstr + "<table>";

			Objectify ofy = ObjectifyService.begin();
			Query<Area> q = ofy.query(Area.class).order("areaIndex");
			for ( Area f : q.fetch() ) {
				Query<SolutionSubscription> qq = ofy.query(SolutionSubscription.class).filter("areaID", f.getAreaID()).filter("subscribeBY", userEmailID);
				subscribed = false;
				for ( SolutionSubscription ff : qq.fetch() ) {
					subscribed = true;
					break;
				}
				if ( row == 2) {
					htmlstr	=	htmlstr +	"<TR>";
				}

				if (subscribed)
				{
					htmlstr	=	htmlstr +	"<TD><INPUT id=\"chk"+ f.getAreaIndex() +"\" onclick=\"UnSubscribeDocValue('" + f.getAreaID() + "','" + f.getAreaIndex() + "')\" type=checkbox name=chk><FONT style='FONT-WEIGHT: bold' color=#fdb813>Subscribed </FONT>" + f.getAreaName() + "</TD>";
				}else{
					htmlstr	=	htmlstr +	"<TD><INPUT id=\"chk"+ f.getAreaIndex() +"\" onclick=\"StoreDocValue('" + f.getAreaID() + "','" + f.getAreaIndex() + "')\" type=checkbox name=chk>" + f.getAreaName() + "</TD>";
				}
				
				row++;
				
				if ( row == 4){
					htmlstr	=	htmlstr +	"</TR>";
					row = 0;
				}

			}
			
		}else if (KeyTemp.equalsIgnoreCase("BenefitsInfo")) {
			boolean firstTime = true;
			String lastBenefitType = "";
			
			htmlstr = 	"<div class='lotusTabContainer'  ><ul class='lotusTabs'><li><a  href='javascript:CustomersInfo()'>Customers</a></li><li><a href='javascript:ProcessInfo()' >Process Area</a></li><li class ='lotusSelected'><a  href='javascript:BenefitsInfo()'>Benefits</a></li><li ><a href='javascript:SitesInfo()'>Locations</a></li></ul></div>";		
			
			

			htmlstr	=	htmlstr	+	"<table>";
			Objectify ofy = ObjectifyService.begin();
			//Query<Benefit> q = ofy.query(Benefit.class).order("benefitIndex").order("benefitType");
			Query<Benefit> q = ofy.query(Benefit.class).order("benefitIndex");
			//q.order("benefitType");
			for ( Benefit f : q.fetch() ) {
				
				if (firstTime){
					lastBenefitType = (String) f.getBenefitType();
					firstTime = false;
					htmlstr	=	htmlstr +	"<TR>";
					htmlstr	=	htmlstr	+ 	"<td><br><b><font style='font-weight:bold;font-size:13px;font-family:Arial'>" + f.getBenefitType() + "<br></td>";
					htmlstr	=	htmlstr +	"</TR>";

				}
				
				Query<SolutionSubscription> qq = ofy.query(SolutionSubscription.class).filter("benefitID", f.getBenefitID()).filter("subscribeBY", userEmailID);
				subscribed = false;
				for ( SolutionSubscription ff : qq.fetch() ) {
					subscribed = true;
					break;
				}
				
				if ( !f.getBenefitType().equalsIgnoreCase(lastBenefitType) ){
					htmlstr	=	htmlstr +	"<TR>";
					htmlstr	=	htmlstr	+ 	"<td><br><b><font style='font-weight:bold;font-size:13px;font-family:Arial'>" + f.getBenefitType() + "<br></td>";
					htmlstr	=	htmlstr +	"</TR>";
					lastBenefitType = f.getBenefitType();
				}
				
				if ( row == 2) {
					htmlstr	=	htmlstr +	"<TR>";
				}

				if (subscribed)
				{
					htmlstr	=	htmlstr +	"<TD><INPUT id=\"chk"+ f.getBenefitIndex() +"\" onclick=\"UnSubscribeDocValue('" + f.getBenefitID() + "','" + f.getBenefitIndex() + "')\" type=checkbox name=chk><FONT style='FONT-WEIGHT: bold' color=#fdb813>Subscribed </FONT>" + f.getBenefitName() + "</TD>";
				}else{
					htmlstr	=	htmlstr +	"<TD><INPUT id=\"chk"+ f.getBenefitIndex() +"\" onclick=\"StoreDocValue('" + f.getBenefitID() + "','" + f.getBenefitIndex() + "')\" type=checkbox name=chk>" + f.getBenefitName() + "</TD>";
				}
				
				row++;
				
				if ( row == 4){
					htmlstr	=	htmlstr +	"</TR>";
					row = 0;
				}

			}			
		}else if (KeyTemp.equalsIgnoreCase("SitesInfo")) {
			boolean firstTime = true;
			String lastRegionName = "";
			
			htmlstr = 	"<div class='lotusTabContainer'  ><ul class='lotusTabs'><li><a  href='javascript:CustomersInfo()'>Customers</a></li><li><a href='javascript:ProcessInfo()' >Process Area</a></li><li><a  href='javascript:BenefitsInfo()'>Benefits</a></li><li class ='lotusSelected'><a href='javascript:SitesInfo()'>Locations</a></li></ul></div>";		
			
			

			htmlstr	=	htmlstr	+	"<table>";
			Objectify ofy = ObjectifyService.begin();
			Query<Location> q = ofy.query(Location.class).order("LocationRegionID");
			for ( Location f : q.fetch() ) {
				
				if (firstTime){
					lastRegionName = (String) f.getLocationRegionName();
					firstTime = false;
					htmlstr	=	htmlstr +	"<TR>";
					htmlstr	=	htmlstr	+ 	"<td><br><b><font style='font-weight:bold;font-size:13px;font-family:Arial'>" + f.getLocationRegionName() + "<br></td>";
					htmlstr	=	htmlstr +	"</TR>";					
				}
				
				Query<SolutionSubscription> qq = ofy.query(SolutionSubscription.class).filter("locID", f.getLocID()).filter("subscribeBY", userEmailID);
				subscribed = false;
				for ( SolutionSubscription ff : qq.fetch() ) {
					subscribed = true;
					break;
				}
				
				if ( !f.getLocationRegionName().equalsIgnoreCase(lastRegionName) ){
					htmlstr	=	htmlstr +	"<TR>";
					htmlstr	=	htmlstr	+ 	"<td><br><b><font style='font-weight:bold;font-size:13px;font-family:Arial'>" + f.getLocationRegionName() + "<br></td>";
					htmlstr	=	htmlstr +	"</TR>";
					lastRegionName = f.getLocationRegionName();
				}
				
				if ( row == 2) {
					htmlstr	=	htmlstr +	"<TR>";
				}

				if (subscribed)
				{
					htmlstr	=	htmlstr +	"<TD><INPUT id=\"chkLocations"+ f.getLocationIndex() +"\" onclick=\"UnSubscribeLocDocValue('" + f.getLocID() + "','" + f.getLocationIndex() + "')\" type=checkbox name=chk><FONT style='FONT-WEIGHT: bold' color=#fdb813>Subscribed </FONT>" + f.getLocationName() + "</TD>";
				}else{
					htmlstr	=	htmlstr +	"<TD><INPUT id=\"chkLocations"+ f.getLocationIndex() +"\" onclick=\"StoreValue('" + f.getLocID() + "','" + f.getLocationIndex() + "')\" type=checkbox name=chk>" + f.getLocationName() + "</TD>";
				}
				
				row++;
				
				if ( row == 4){
					htmlstr	=	htmlstr +	"</TR>";
					row = 0;
				}

			}			
			
		}
		
		htmlstr	=	htmlstr	+	"</table><br><br>";
		//htmlstr	=	htmlstr	+	"<input type='button' class='lotusBtn lotusBtnAction lotusLeft' value = 'Subscribe' onclick='javascript:savedoc()'><input type='button' class='lotusBtn lotusBtnAction lotusLeft' value='UnSubscribe' onclick='javascript:Unsavedoc()'><input type='button' class='lotusBtn lotusBtnAction lotusLeft' value='Cancel' onclick='javascript:canceldoc()'>";
		
		//System.out.println(htmlstr);
		out.println(htmlstr);
	}
}
